package com.dfiov.cloud.msmtools.mapper.entity.msbluetooth;

import com.dfiov.cloud.msmtools.contant.msbluetooth.BleAuthStatusEnum;
import com.dfiov.cloud.msmtools.contant.msbluetooth.BleKeyStatusEnum;
import com.dfiov.cloud.msmtools.entity.vo.msbluetooth.KeyPermission;
import com.dfiov.cloud.msmtools.entity.vo.msmtools.UserBaseInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
public class D53ApplyResp {

    @ApiModelProperty("钥匙主键ID")
    private Long id;

    @ApiModelProperty("deviceId")
    private String deviceId;

    @ApiModelProperty("蓝牙钥匙敏感信息")
    private BluetoothSensitiveInfo bluetoothSensitiveInfo;

    @ApiModelProperty("用户公钥")
    private String userPubKey;

    @ApiModelProperty("tbox公钥")
    private String pubKey;

    @ApiModelProperty("钥匙状态1有效、2注销")
    private Integer keyStatus;

    @ApiModelProperty("创建时间")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private String createTime;

    @ApiModelProperty("车主标识 1 车主  0 非车主")
    private Integer relationship;

    @ApiModelProperty("蓝牙钥匙本地id")
    private String bleKeyId;

    @ApiModelProperty(" app端蓝牙钥匙状态 0 临时钥匙 1 永久钥匙")
    private Integer bleLocalStatus;

    @ApiModelProperty("用户账号sid")
    private Long userAccountSid;

    @ApiModelProperty("移动设备号")
    private String mobileDeviceNo;

    @ApiModelProperty("用户信息")
    private UserBaseInfo userDetail;

    @ApiModelProperty("钥匙权限")
    private KeyPermission permission;

    @ApiModelProperty("借车人手机号")
    private String borrowerPhone;

    @ApiModelProperty("授权码")
    private String authCode;

    @ApiModelProperty("授权有效期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime authExpireTime;

    @ApiModelProperty("是否已接受授权 0 否 1是")
    private Integer isAuthAccepted;

    @ApiModelProperty("授权确认时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime authAcceptedTime;

    @ApiModelProperty("授权确认用户ID")
    private Long authAcceptedUser;

    @ApiModelProperty("授权状态")
    private Integer authStatus;

    @ApiModelProperty("tbox公钥加密")
    private String authVoucher;

    @ApiModelProperty("sign")
    private String authSignature;

    @ApiModelProperty("实际加密字段")
    private String encryptInfo;

    @ApiModelProperty("tbox公钥加密")
    private String authVoucherCiphertext;


    @ApiModelProperty("车主id")
    private String masterId;

    @JsonIgnore
    public void initEncryptInfo() {
        this.encryptInfo = bluetoothSensitiveInfo.getEncryptInfo(userPubKey);
    }

    @JsonIgnore
    public void initAuthStatus() {
        // 非车主钥匙-未获取授权
        // 非车主钥匙-已经获取授权
        // 非车主钥匙-已激活
        if (relationship == 1 || keyStatus.equals(BleKeyStatusEnum.AVAILABLE.getValue())) {
            authStatus = BleAuthStatusEnum.KEYACTIVE.getValue();
        } else if (keyStatus.equals(BleKeyStatusEnum.UNUSE.getValue())) {
            if (StringUtils.isBlank(borrowerPhone)) {
                authStatus = BleAuthStatusEnum.SHARE.getValue();
            } else if (0 == isAuthAccepted) {
                authStatus = BleAuthStatusEnum.AUTHNOTUSED.getValue();
            } else if (0 == isAuthAccepted || authAcceptedTime != null || authAcceptedUser != null) {
                authStatus = BleAuthStatusEnum.AUTHUSED.getValue();
            }
        } else {
            authStatus = BleAuthStatusEnum.KEYDELETE.getValue();
        }

    }

    public static D53ApplyResp d53fromBean(BluetoothKeyCompleteInfoD53 bkci, String userPubKey) {
        D53ApplyResp obj = D53ApplyResp.builder().id(bkci.getId()).deviceId(bkci.getDeviceId())
                .pubKey(bkci.getDevicePublicKey())
                .userPubKey(StringUtils.isNotBlank(userPubKey) ? userPubKey : bkci.getUserPublicKey())
                .createTime(bkci.getCreatetime())
                .bluetoothSensitiveInfo(BluetoothSensitiveInfo.builder()
                        .bluetoothAccessKey(bkci.getBluetoothAccessKey())
                        .bluetoothConKey(bkci.getBluetoothConKey())
                        .bluetoothConName(bkci.getBluetoothConName())
                        .keyExpireTime(bkci.getKeyExpireTime())
                        .build())
                .relationship(bkci.getRelationship())
                .bleKeyId(bkci.getBleLocalKeyId())
                .userAccountSid(bkci.getUserAccountSid())
                .bleLocalStatus(bkci.getBleLocalStatus())
                .permission(bkci.getPermission())
                .borrowerPhone(bkci.getBorrowerPhone())
                .isAuthAccepted(bkci.getIsAuthAccepted())
                .authExpireTime(bkci.getAuthExpireTime())
                .authAcceptedTime(bkci.getAuthAcceptedTime())
                .authAcceptedUser(bkci.getAuthAcceptedUser())
                .keyStatus(bkci.getKeyStatus())
                .authCode(bkci.getAuthCode())
                .mobileDeviceNo(bkci.getMobileDeviceNo())
                .build();
        obj.initEncryptInfo();
        return obj;
    }


    public static D53ApplyResp d53fromBeanNoUserPubKey(BluetoothKeyCompleteInfoD53 bkci) {
        D53ApplyResp obj = D53ApplyResp.builder().id(bkci.getId()).deviceId(bkci.getDeviceId())
                .pubKey(bkci.getDevicePublicKey())
                .createTime(bkci.getCreatetime())
                .bluetoothSensitiveInfo(BluetoothSensitiveInfo.builder()
                        .bluetoothAccessKey(bkci.getBluetoothAccessKey()).bluetoothConKey(bkci.getBluetoothConKey())
                        .bluetoothConName(bkci.getBluetoothConName()).keyExpireTime(bkci.getKeyExpireTime()).build())
                .relationship(bkci.getRelationship()).bleKeyId(bkci.getBleLocalKeyId())
                .userAccountSid(bkci.getUserAccountSid()).bleLocalStatus(bkci.getBleLocalStatus())
                .permission(bkci.getPermission()).borrowerPhone(bkci.getBorrowerPhone())
                .isAuthAccepted(bkci.getIsAuthAccepted()).authExpireTime(bkci.getAuthExpireTime())
                .authAcceptedTime(bkci.getAuthAcceptedTime()).authAcceptedUser(bkci.getAuthAcceptedUser())
                .keyStatus(bkci.getKeyStatus()).authCode(bkci.getAuthCode()).mobileDeviceNo(bkci.getMobileDeviceNo())
                .authVoucher(bkci.getAuthVoucher()).authSignature(bkci.getAuthSignature()).masterId(bkci.getMasterId())
                .build();
        return obj;
    }
}
